import { fileURLToPath, URL } from "node:url"
import { loadEnv, type UserConfig } from "vite"
import type { ImportMetaEnv } from "./types/env"

/** @type { import('vite').defineConfig} */
export default (mode: string): UserConfig => {
  const env = loadEnv(mode, fileURLToPath(new URL("./", import.meta.url))) as unknown as ImportMetaEnv
  return {
    server: {
      host: "0.0.0.0",
      port: 8080,
      open: "true".includes(env.VITE_OPEN),
      proxy: {
        "/api": {
          target: env.VITE_BASE_BS_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, "")
        }
      }
    }
  }
}
